package com.chen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.common.Constants;
import com.chen.entity.Request;
import com.chen.entity.User;
import com.chen.exception.ServiceException;
import com.chen.mapper.RequestMapper;
import com.chen.mapper.UserMapper;
import com.chen.service.IRequestService;
import com.chen.utils.TokenUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Y·C
 * @since 2023-04-21
 */
@Service
public class RequestServiceImpl extends ServiceImpl<RequestMapper, Request> implements IRequestService {

    @Resource
    private RequestMapper requestMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public void requestInfo(Request request) {
        User user = TokenUtils.getCurrentUser();
        request.setUid(user.getId());
        int i = requestMapper.insert(request);
        if (i<1){
            throw new ServiceException(Constants.CODE_600, "填写失败");
        }
    }

    @Override
    public void updateRequestInfo(Request request) {
        QueryWrapper<Request> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("uid",request.getUid());
        int i = requestMapper.update(request,objectQueryWrapper);
        if (i<1){
            throw new ServiceException(Constants.CODE_600, "填写失败");
        }
    }

    @Override
    public List<User> queryWedding(String maritalStatus) {
        QueryWrapper<User> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("hunyin_state",maritalStatus);
        return userMapper.selectList(objectQueryWrapper);
    }

    @Override
    public List<User> queryAge(int minAge, int maxAge)  {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            String time = dateFormat.format(date);
            String min = String.valueOf(2023- minAge);
            String max = String.valueOf(2023- maxAge);
            min = time.replace("2023",min);
            max = time.replace("2023",max);
            QueryWrapper<User> objectQueryWrapper = new QueryWrapper<>();
            objectQueryWrapper.le("birthday",min);
            objectQueryWrapper.ge("birthday",max);
            List<User> users1 = userMapper.selectList(objectQueryWrapper);
            return users1;
        }catch (Exception e){
            return null;
        }
    }

    @Override
    public List<User> queryHeight(Double low, Double high) {
        QueryWrapper<User> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.le("height",high);
        objectQueryWrapper.ge("height",low);
        List<User> users = userMapper.selectList(objectQueryWrapper);
        return users;
    }

    @Override
    public List<User> queryXueli(int xueli) {
        QueryWrapper<User> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.ge("xueli",xueli);
        List<User> users = userMapper.selectList(objectQueryWrapper);
        return users;
    }

    @Override
    public List<User> queryIncome(int income) {
        QueryWrapper<User> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.ge("month_income",income);
        List<User> users = userMapper.selectList(objectQueryWrapper);
        return users;
    }

    @Override
    public List<User> queryHouse(String house) {
        QueryWrapper<User> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("house_state",house);
        List<User> users = userMapper.selectList(objectQueryWrapper);
        return users;
    }

    @Override
    public List<User> queryCar(String car) {
        QueryWrapper<User> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("cart_state",car);
        List<User> users = userMapper.selectList(objectQueryWrapper);
        return users;
    }

    @Override
    public List<User> queryHuji(String huji) {
        QueryWrapper<User> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.like("huji_place",huji);
        List<User> users = userMapper.selectList(objectQueryWrapper);
        return users;
    }

    @Override
    public Request queryRequest(int id) {
        QueryWrapper<Request> requestQueryWrapper = new QueryWrapper<>();
        requestQueryWrapper.eq("uid",id);
        Request request = requestMapper.selectOne(requestQueryWrapper);
        return request;
    }

    @Override
    public User queryUser(int id) {
        User user = userMapper.selectById(id);
        return user;
    }
    @Override
    public List<User> querySex(String sex){
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("sex",sex);
        return userMapper.selectList(userQueryWrapper);
    }

}
